package com.itheima.edu.info.temanager.service;

import com.itheima.edu.info.temanager.dao.TeacherDao;
import com.itheima.edu.info.temanager.domain.Teacher;

public class TeacherService {
    private TeacherDao teacherDao = new TeacherDao();

    public boolean addTeacher(Teacher te) {
        //暂时不进行判断学号是否存在


        return teacherDao.addTeacher(te);
    }


    public boolean isExists(String id) {
        Teacher[] teaArray = teacherDao.findAllTeacher();
        //利用标记位思想
        boolean flag = false;
        //遍历数组,将数组的对象用Teacher接收,并比较id是否相同
        for (int i = 0; i < teaArray.length; i++) {
            Teacher teacher = teaArray[i];
            if (teacher != null && teacher.getId().equals(id)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    public Teacher[] findAllTeacher() {
        //创建一个数组接收传回的数组,接下来用于逻辑分析
        Teacher[] teachers = teacherDao.findAllTeacher();
        //运用标记位的思想,假定传回来的啥也没有
        boolean flag = false;
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher != null) {
                flag = true;
                break;
            }
        }
        if (flag){
            //true:返回数组
            return teachers;
        }else {
            return null;
        }
    }
}